<?php
use \Model\Target;
use \Model\Account;
use \Model\Msg;
use \Model\Queue;
use \Config;
class Controller_update extends Controller_Weibo{
	static function touch_target($screen_name) {
		$data['screen_name'] = $screen_name;
		$data['touched_at'] = date('Y-m-d H:i:s', time());
		Target::update($data);
	}
	static function update_target($screen_name) {
		$access_token = Account::get_access_token();
		$c = new \SaeTClientV2( Config::get('weibo.WB_AKEY', ''), Config::get('weibo.WB_SKEY', ''), $access_token);
		$user_message = $c->show_user_by_name( $screen_name);//根据昵称获取用户等基本信息
		if (!isset($user_message['name']) or !$user_message['name']) {
				return self::msg(array('msg' => 'fail', 'data' => array()));
		}
		$data['name'] = $user_message['name'];
		$data['screen_name'] = $user_message['screen_name'];
		$data['idstr'] = $user_message['idstr'];
		$data['profile_image_url'] = $user_message['profile_image_url'];
		$data['url'] = $user_message['url'];
		$data['location'] = $user_message['location'];
		$data['description'] = $user_message['description'];
		$data['followers_count'] = $user_message['followers_count'];
		$data['friends_count'] = $user_message['friends_count'];
		$data['statuses_count'] = $user_message['statuses_count'];
		$data['updated_at'] = date('Y-m-d H:i:s', time());
		Target::update($data);
	}

	static function update_msg($screen_name) {
		$access_token = Account::get_access_token();
		$c = new \SaeTClientV2( Config::get('weibo.WB_AKEY', ''), Config::get('weibo.WB_SKEY', ''), $access_token);
		$user_message = $c->show_user_by_name( $screen_name);//根据昵称获取用户等基本信息
		if (!isset($user_message['name']) or !$user_message['name']) {
				return self::msg(array('msg' => 'fail', 'data' => array()));
		}
		$msg = $c->user_timeline_by_name($screen_name, 1, 10);
		foreach ($msg['statuses'] as $k => $v) {
			$data['sina_id'] = $v['id'];
			$data['text'] = $v['text'];
			$data['reposts_count'] = $v['reposts_count'];
			$data['comments_count'] = $v['comments_count'];
			$data['idstr'] = $v['user']['id'];
			$data['screen_name'] = $v['user']['screen_name'];
			$data['name'] = $v['user']['name'];
			$data['profile_image_url'] = $v['user']['profile_image_url'];
			$data['created_at'] = date('Y-m-d H:i:s', strtotime($v['created_at']) + 8*60*60);
			$data['original_pic'] = isset($v['original_pic']) ? $v['original_pic'] : '';

			if (isset($v['retweeted_status'])) {
				$data['re_sina_id'] = $v['retweeted_status']['id'];
				$data['re_text'] = $v['retweeted_status']['text'];
				$data['re_reposts_count'] = $v['retweeted_status']['reposts_count'];
				$data['re_comments_count'] = $v['retweeted_status']['comments_count'];
				$data['re_idstr'] = $v['retweeted_status']['user']['id'];
				$data['re_screen_name'] = $v['retweeted_status']['user']['screen_name'];
				$data['re_name'] = $v['retweeted_status']['user']['name'];
				$data['re_profile_image_url'] = $v['retweeted_status']['user']['profile_image_url'];
				$data['re_created_at'] = date('Y-m-d H:i:s', strtotime($v['retweeted_status']['created_at']) + 8*60*60);
				$data['re_original_pic'] = isset($v['retweeted_status']['original_pic']) ? $v['retweeted_status']['original_pic'] : '';
			}
			Msg::add($data);
			unset($data);
		}
	}

	function action_account() {
		$o = new SaeTOAuthV2( Config::get('weibo.WB_AKEY', ''), Config::get('weibo.WB_SKEY', ''));
		$account = Account::get_account();
		foreach ($account as $k => $v) {
			try {
				$token = $o->getAccessToken('password', array('username' => $v['username'], 'password' => $v['password']));
			} catch (Exception $e){
				Log::error('update account failed.');
				Log::info($e);
			}
			if (isset($token['access_token']) and $token['access_token']) {
				$c = new SaeTClientV2( Config::get('weibo.WB_AKEY', ''), Config::get('weibo.WB_SKEY', ''), $token['access_token']);
				$user_message = $c->show_user_by_id( $token['uid']);//根据ID获取用户等基本信息
				if (!isset($user_message['name']) or !$user_message['name']) {
					return self::msg(array('msg' => 'fail', 'data' => array()));
				}
				$data['name'] = $user_message['name'];
				$data['screen_name'] = $user_message['screen_name'];
				$data['idstr'] = $user_message['idstr'];
				$data['profile_image_url'] = $user_message['profile_image_url'];
				$data['url'] = $user_message['url'];
				$data['location'] = $user_message['location'];
				$data['description'] = $user_message['description'];
				$data['access_token'] = $token['access_token'];
				$data['password'] = $v['password'];
				$data['followers_count'] = $user_message['followers_count'];
				$data['friends_count'] = $user_message['friends_count'];
				$data['statuses_count'] = $user_message['statuses_count'];
				Account::update($data);
			} else {
				Log::error('update account failed.');
				Log::info('token => ' . $token);
			}
		}
	}

	function action_msg() {
		$target = Target::get_all_account();
		foreach ($target as $k => $v) {
			self::update_msg($v['screen_name']);
			self::touch_target($v['screen_name']);
		}
	}

	function action_queue() {
		$queue = Queue::get_queue(Session::get('user_id', 0));
		foreach ($queue as $q) {
				$access_token = Account::get_access_token_by_screen_name($q['screen_name']);
				$c = new \SaeTClientV2( Config::get('weibo.WB_AKEY', ''), Config::get('weibo.WB_SKEY', ''), $access_token);
				if (strtotime($q['post_time']) <= time() + 8*60*60 and $q['status'] == 0){
						if ($q['image'] != '') {
								$r = $c->upload($q['text'], $q['image']);
						} else {
								$r = $c->update($q['text'], $q['image']);
						}
						$now = date('Y-m-d H:i:s', time() + 8 * 60 * 60);
						if (!isset($r['error'])) {
								Queue::update(array('id' => $q['id'], 'status' => 1, 'post_time' => $now, 'sina_user_idstr' => $r['user']['id'], 'post_id' => $r['id']));
						} else {
								Queue::update(array('id' => $q['id'], 'status' => 2, 'post_time' => $now, 'sina_user_idstr' => $r['user']['id'], 'post_id' => $r['id']));
						}
				}
				if (strtotime($q['post_time']) <= time() + 8*60*60 and $q['status'] == 4){
					if (strstr($q['post_id'], '.')) {
						$c->destroy(strstr($q['post_id'], '.', true));
					} else {
						$c->destroy($q['post_id']);
					}
					//Queue::update(array('id' => $q['id'], 'status' => 5, 'post_time' => $now, 'sina_user_idstr' => $r['user']['id'], 'post_id' => $r['id']));
				}
		}
	}
	function after($response) {
		return 'updated .';
	}

}
